package handler

import (
	"admin-nexus-backend/internal/dto"
	"admin-nexus-backend/internal/service"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

// DictHandler 字典处理器
type DictHandler struct {
	dictService *service.DictService
}

// NewDictHandler 创建字典处理器实例
func NewDictHandler(dictService *service.DictService) *DictHandler {
	return &DictHandler{
		dictService: dictService,
	}
}

// ============ 字典类型接口 ============

// GetDictTypeList 获取字典类型列表
// @Summary 获取字典类型列表
// @Tags 字典管理
// @Accept json
// @Produce json
// @Param dictCode query string false "字典编码"
// @Param dictName query string false "字典名称"
// @Param status query int false "状态"
// @Param page query int true "页码"
// @Param pageSize query int true "每页数量"
// @Success 200 {object} map[string]interface{}
// @Router /api/dict/type/list [get]
func (h *DictHandler) GetDictTypeList(c *gin.Context) {
	var req dto.DictTypeListRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
		return
	}

	list, total, err := h.dictService.GetDictTypeList(req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "获取字典类型列表失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "success",
		"data": gin.H{
			"list":  list,
			"total": total,
		},
	})
}

// GetDictTypeByID 根据ID获取字典类型
// @Summary 根据ID获取字典类型
// @Tags 字典管理
// @Accept json
// @Produce json
// @Param id path int true "字典类型ID"
// @Success 200 {object} map[string]interface{}
// @Router /api/dict/type/{id} [get]
func (h *DictHandler) GetDictTypeByID(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "无效的ID"})
		return
	}

	dictType, err := h.dictService.GetDictTypeByID(uint(id))
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"code": 404, "message": "字典类型不存在"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "success",
		"data":    dictType,
	})
}

// CreateDictType 创建字典类型
// @Summary 创建字典类型
// @Tags 字典管理
// @Accept json
// @Produce json
// @Param body body dto.CreateDictTypeRequest true "字典类型信息"
// @Success 200 {object} map[string]interface{}
// @Router /api/dict/type [post]
func (h *DictHandler) CreateDictType(c *gin.Context) {
	var req dto.CreateDictTypeRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
		return
	}

	dictType, err := h.dictService.CreateDictType(req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "创建字典类型失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "创建成功",
		"data":    dictType,
	})
}

// UpdateDictType 更新字典类型
// @Summary 更新字典类型
// @Tags 字典管理
// @Accept json
// @Produce json
// @Param id path int true "字典类型ID"
// @Param body body dto.UpdateDictTypeRequest true "字典类型信息"
// @Success 200 {object} map[string]interface{}
// @Router /api/dict/type/{id} [put]
func (h *DictHandler) UpdateDictType(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "无效的ID"})
		return
	}

	var req dto.UpdateDictTypeRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
		return
	}

	dictType, err := h.dictService.UpdateDictType(uint(id), req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "更新字典类型失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "更新成功",
		"data":    dictType,
	})
}

// DeleteDictType 删除字典类型
// @Summary 删除字典类型
// @Tags 字典管理
// @Accept json
// @Produce json
// @Param id path int true "字典类型ID"
// @Success 200 {object} map[string]interface{}
// @Router /api/dict/type/{id} [delete]
func (h *DictHandler) DeleteDictType(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "无效的ID"})
		return
	}

	if err := h.dictService.DeleteDictType(uint(id)); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "删除字典类型失败，可能存在关联数据"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "删除成功",
	})
}

// ============ 字典数据接口 ============

// GetDictDataList 获取字典数据列表
// @Summary 获取字典数据列表
// @Tags 字典管理
// @Accept json
// @Produce json
// @Param dictCode query string false "字典类型编码"
// @Param status query int false "状态"
// @Param page query int true "页码"
// @Param pageSize query int true "每页数量"
// @Success 200 {object} map[string]interface{}
// @Router /api/dict/data/list [get]
func (h *DictHandler) GetDictDataList(c *gin.Context) {
	var req dto.DictDataListRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
		return
	}

	list, total, err := h.dictService.GetDictDataList(req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "获取字典数据列表失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "success",
		"data": gin.H{
			"list":  list,
			"total": total,
		},
	})
}

// GetDictDataByCode 根据字典编码获取字典数据（用于前端下拉框）
// @Summary 根据字典编码获取字典数据
// @Tags 字典管理
// @Accept json
// @Produce json
// @Param dictCode query string true "字典类型编码"
// @Success 200 {object} map[string]interface{}
// @Router /api/dict/data/code [get]
func (h *DictHandler) GetDictDataByCode(c *gin.Context) {
	dictCode := c.Query("dictCode")
	if dictCode == "" {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "dictCode参数不能为空"})
		return
	}

	list, err := h.dictService.GetDictDataByCode(dictCode)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "获取字典数据失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "success",
		"data":    list,
	})
}

// GetDictDataByID 根据ID获取字典数据
// @Summary 根据ID获取字典数据
// @Tags 字典管理
// @Accept json
// @Produce json
// @Param id path int true "字典数据ID"
// @Success 200 {object} map[string]interface{}
// @Router /api/dict/data/{id} [get]
func (h *DictHandler) GetDictDataByID(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "无效的ID"})
		return
	}

	dictData, err := h.dictService.GetDictDataByID(uint(id))
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"code": 404, "message": "字典数据不存在"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "success",
		"data":    dictData,
	})
}

// CreateDictData 创建字典数据
// @Summary 创建字典数据
// @Tags 字典管理
// @Accept json
// @Produce json
// @Param body body dto.CreateDictDataRequest true "字典数据信息"
// @Success 200 {object} map[string]interface{}
// @Router /api/dict/data [post]
func (h *DictHandler) CreateDictData(c *gin.Context) {
	var req dto.CreateDictDataRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
		return
	}

	dictData, err := h.dictService.CreateDictData(req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "创建字典数据失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "创建成功",
		"data":    dictData,
	})
}

// UpdateDictData 更新字典数据
// @Summary 更新字典数据
// @Tags 字典管理
// @Accept json
// @Produce json
// @Param id path int true "字典数据ID"
// @Param body body dto.UpdateDictDataRequest true "字典数据信息"
// @Success 200 {object} map[string]interface{}
// @Router /api/dict/data/{id} [put]
func (h *DictHandler) UpdateDictData(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "无效的ID"})
		return
	}

	var req dto.UpdateDictDataRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
		return
	}

	dictData, err := h.dictService.UpdateDictData(uint(id), req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "更新字典数据失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "更新成功",
		"data":    dictData,
	})
}

// DeleteDictData 删除字典数据
// @Summary 删除字典数据
// @Tags 字典管理
// @Accept json
// @Produce json
// @Param id path int true "字典数据ID"
// @Success 200 {object} map[string]interface{}
// @Router /api/dict/data/{id} [delete]
func (h *DictHandler) DeleteDictData(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "无效的ID"})
		return
	}

	if err := h.dictService.DeleteDictData(uint(id)); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "删除字典数据失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "删除成功",
	})
}
